var ActionsManager;
$(function() {
	var typeData = [{ type: 1, text: '功能栏目' }, { type: 2, text: '功能项'}];
	
	var toolBarItems = [ {
		text : '保存',
		click : saveUpdateActions,
		icon : 'modify'
	}, {
		line : true
	}, {
		text : '增加',
		click : itemActionsClick,
		icon : 'add'
	}, {
		line : true
	}];
	
	if(pidValue != 0){
		toolBarItems.push( {
				text : '返回',
				click : itemActionsBack,
				icon : 'back'
			});
		toolBarItems.push({
				line : true
			});
	}
	
	ActionsManager = window['g'] = $("#maingrid").ligerGrid({
		height : '100%',
		url:'/v1/a/getActions?pid='+pidValue,
		columns : [ {
			display : '编号',
			name : 'id',
			align : 'left',
			showTitle : true,
			minWidth : 100,
			align : "center",
			isSort : false
		}, {
			display : '功能名',
			name : 'name',
			minWidth : 100,
			align : "center",
			editor: { type: 'text' },
			isSort : false
		},{
			display : '功能地址',
			name : 'url',
			minWidth : 150,
			align : "center",
			editor: { type: 'text' },
			isSort : false
		},{
			display : '类型',
			name : 'type',
			minWidth : 20,
			align : "center",
			editor: { type: 'select', data: typeData, valueColumnName: 'type' },
            render: function (item)
            {
                 if (parseInt(item.type) == 1){
                	 return '功能栏目';
                 }else{
                	 return '功能项';
                 }
            },
			isSort : false
		}],
		pageSize : 10,
		rownumbers : true,
		toolbar : {
			items : toolBarItems
		},
		onDblClickRow: function (rowdata, rowindex, rowDomElement)
        {
           if(rowdata.id){
        	   if(1 == rowdata.type){
        		   location.href="/v1/a/index?pid="+rowdata.id;
        	   }else{
        		   $.ligerDialog.error("功能项没有子功能哦!");
        	   }
        	   
           }
        },
        enabledEdit: true, 
        isScroll: false, 
        rownumbers:true
	});
    
    jQuery("#pageloading").hide();
});
function getUpdate()
{
    var data = ActionsManager.getUpdated();
    alert(JSON.stringify(data));
    return data;
}

function saveUpdateActions(item){
	var data = ActionsManager.getUpdated();
	var addData = ActionsManager.getAdded();
	if(addData.length > 0){
		for(var i=0;i<addData.length;i++){
			data.push(addData[i]);
		}
	}
	if(data.length == 0){
		$.ligerDialog.warn("亲，您还未做任何修改哦!");
		return;
	}
	doSaveActions(data);
}
function doSaveActions(data,add){
	jQuery.ajax({
		url:'/v1/a/saveOrUpdate',
		data:{json:JSON.stringify(data)},
		dataType:'json',
		type:'post',
		success:function(data){
			if(data.sucessList.length > 0){
				$.ligerDialog.success('亲,已经保存成功!');
				tip = $.ligerDialog.tip({ title: '提示信息', content: '亲,已经保存'+data.sucessList.length+'条记录!' });
				ActionsManager.loadServerData();
			}else{
				tip = $.ligerDialog.tip({ title: '提示信息', content: '网络异常,没有保存成功!' });
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
}
function itemActionsClick(item) {
	ActionsManager.addRow({ 
		pid:pidValue
    },0);
}
function itemActionsBack(item){
	location.href="/v1/a/reParent?pid="+pidValue;
}